const fs = require('fs')
const IniConfigParser = require('ini-config-parser')
const ConfigFile = 'etc/config.conf'
const DefaultPort = 3000

function ConfigManager (path) {
  // 参数必须赋值，否则抛出异常
  if (!path || !fs.existsSync(path)) {
    throw new Error(`lack of falcon config file ${path}`)
  }

  const data = fs.readFileSync(path).toString()
  this.config = IniConfigParser.parse(data)
}

ConfigManager.prototype = {
  constructor: ConfigManager,

  getPort: function () {
    const port = this.config.demo.port
    if (!port) {
      return DefaultPort
    }
    return port
  },

  getLoggerDir: function () {
    const loggerDir = this.config.logger.dir
    return loggerDir
  },

  getWordPath: function () {
    const wordpath = this.config.wordtopdf.wordpath
    return wordpath
  },

  getPdfPath: function () {
    const pdfdpath = this.config.wordtopdf.pdfpath
    return pdfdpath
  },

  getAppid: function () {
    return this.config.Applets.appID
  },

  getAppSecret: function () {
    return this.config.Applets.appSecret
  },

  getDbFile: function () {
    return this.config.db.db_dir
  },

  getSecretKey: function () {
    return this.config.secretkey.secretKey
  },

  getprintUrl: function () {
    return this.config.printURL.printUrl
  }

}
module.exports = new ConfigManager(ConfigFile)
